<?php


include_once(dirname(__FILE__)."/../testconfig.inc.php");

class Test30 extends UnitTestCase
{
  function testUpload()
  {
    $local_directory=dirname(__FILE__).'/testfiles/';

    $template_dir = dirname(__FILE__)."/../../templates/";
    $destination_dir = $template_dir."bewerbungsmappe";
    if (file_exists($destination_dir))
    {
      $dir_listing = dir($destination_dir);
      while(($dir_entry = $dir_listing->read()) !== false)
      {
        if (($dir_entry != ".") && ($dir_entry != ".."))
        {
          if (is_file($destination_dir."/".$dir_entry))
          {
            unlink($template_dir."bewerbungsmappe"."/".$dir_entry);
          }
        }
      }
      rmdir($destination_dir);
    }
    $this->assertFalse(file_exists($destination_dir));

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_URL, SERVER_TEST_URL.'/030/uploader.php' );
    //most importent curl assues @filed as file field
    $post_array = array("my_file"=>"@".$local_directory.'bewerbungsmappe.zip',"upload"=>"Upload");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array);
    $response = curl_exec($ch);
    echo $response;

    $this->assertTrue(file_exists(dirname(__FILE__)."/../../templates/bewerbungsmappe"),"Directory not created");
    $this->assertTrue(file_exists(dirname(__FILE__)."/../../templates/bewerbungsmappe/babelbst.tex"));
    $this->assertTrue(file_exists(dirname(__FILE__)."/../../templates/bewerbungsmappe/cv.bst"));
    $this->assertTrue(file_exists(dirname(__FILE__)."/../../templates/bewerbungsmappe/cv.dbj"));
    $this->assertTrue(file_exists(dirname(__FILE__)."/../../templates/bewerbungsmappe/cv_german.pdf"));
    $this->assertTrue(file_exists(dirname(__FILE__)."/../../templates/bewerbungsmappe/cv_german.tex"));
    $this->assertTrue(file_exists(dirname(__FILE__)."/../../templates/bewerbungsmappe/letter_german.pdf"));
    $this->assertTrue(file_exists(dirname(__FILE__)."/../../templates/bewerbungsmappe/letter_german.tex"));
    $this->assertTrue(file_exists(dirname(__FILE__)."/../../templates/bewerbungsmappe/moderncv-additions.sty"));
    $this->assertTrue(file_exists(dirname(__FILE__)."/../../templates/bewerbungsmappe/picture.jpg"));
    $this->assertTrue(file_exists(dirname(__FILE__)."/../../templates/bewerbungsmappe/publications.bib"));

  }

  function testUploadNoZip()
  {
    $local_directory=dirname(__FILE__).'/testfiles/';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_URL, SERVER_TEST_URL.'/030/uploader.php' );
    //most importent curl assues @filed as file field
    $post_array = array("my_file"=>"@".$local_directory.'picture.jpg',"upload"=>"Upload");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array);
    $response = curl_exec($ch);
    echo $response;
    $template_dir = dirname(__FILE__)."/../../templates/";
    $this->assertFalse(file_exists($template_dir."picture.jpg"));
    $this->assertFalse(file_exists($template_dir."picture"));
  }

  function testUploadInvalidZip()
  {
    $local_directory=dirname(__FILE__).'/testfiles/';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_URL, SERVER_TEST_URL.'/030/uploader.php' );
    //most importent curl assues @filed as file field
    $post_array = array("my_file"=>"@".$local_directory.'picture.zip',"upload"=>"Upload");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array);
    $response = curl_exec($ch);
    echo $response;
    $template_dir = dirname(__FILE__)."/../../templates/";
    $this->assertFalse(file_exists($template_dir."picture.zip"));
    $this->assertFalse(file_exists($template_dir."picture"));
  }

}
?>